//
//  HMProposalCell.swift
//  HealthMonitoring
//
//  Created by 珠海正杰 on 2018/9/3.
//  Copyright © 2018年 YTYanK. All rights reserved.
//

import UIKit
import SwiftyJSON
/// HMProposalAll 的cell
class HMProposalCell: UITableViewCell {

    var cellHeight: CGFloat = 0.0
    
    var cellBlock: ((_ btn:UIButton) -> (Void))?
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    class func cell(json:JSON, tableView: UITableView, cellForRowAt indexPath: IndexPath) -> HMProposalCell? {
        
        let identifier = "HMProposalCell"
        var cell: HMProposalCell? = tableView.dequeueReusableCell(withIdentifier: identifier) as? HMProposalCell
        if cell == nil {
            cell = HMProposalCell.init(style: .default, reuseIdentifier: identifier)
        }
        cell?.setupViews(json: json, tableView: tableView, view: (cell?.contentView)!, indexPath: indexPath)
        cell?.selectionStyle = UITableViewCellSelectionStyle.none
        return cell
    }
    
    func setupViews(json: JSON, tableView: UITableView, view: UIView, indexPath: IndexPath) {
        

        let matter = UILabel()
        
//        let
       
        let head = TitleHead(self, addView: matter)
        head.setJson(json: ["describe": json["describe"].stringValue])
        view.addSubview(head)
        
        // 中间部分
        //(_ target: Any?, json: JSON)
        let con = DrugsAssembly(self, json: json)
        view.addSubview(con)
        
        matter.centerYAlign(view: head)
        matter.text = json["date"].stringValue
        matter.trailingAlign(view: view, predicate: "0")
        matter.widthConstrain(predicate: "150")
        
        
        con.topVertically(view: head)
        con.leadingAlign(view: view)
        con.trailingAlign(view: view)
        con.heightConstrain(predicate: "\(con.drugsH)")
        
        cellHeight = con.drugsH + 44.0 + 8.0 //con.top= 8
        con.drugsBlock = {
            (btn) -> () in
            self.cellBlock?(btn)
        }
        
 
    }
    
}
